package com.seari.service;

import com.alibaba.fastjson.JSON;
import com.seari.bean.AllFullLoadBean;
import com.seari.bean.CYMaxFullLoadBean;
import com.seari.main.StartupClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName FullLoadServiceImpl
 * @Description TODO
 * @Author LANDSUS
 * @Date 2021/4/29 11:11
 * @Version 1.0
 */
@Service
public class FullLoadServiceImpl implements FullLoadService {

    @Autowired
    private WebClient webClient;

    @Autowired
    private AppCacheService cacheService;

    @Override
    public List<CYMaxFullLoadBean> avgAndMaxFullLoad4Line() {
        String url = StartupClass.config.getProperty("CYMaxFullLoadClient_url");
        Mono<Map> cyMaxFullLoad =
                webClient.get().uri(url).retrieve().bodyToMono(Map.class)
                        .doOnError(WebClientResponseException.class, err -> {
                            throw new RuntimeException("avgAndMaxFullLoad4Line error，error Message:"+err.getMessage());
                        });
        String jsonData = JSON.toJSONString(((Map)cyMaxFullLoad.block().get("data")).get("fullloadList"));
        List<CYMaxFullLoadBean> data = JSON.parseArray(jsonData, CYMaxFullLoadBean.class);
        // 将所有线路的满载率数据存入缓存
        cacheService.refreshCache("fullloadList",data);
        List<CYMaxFullLoadBean> topTen = null;
        if(data!= null && data.size() > 0){
            topTen = data.stream().filter(item -> item.getAvgSaturation() != null)
                    .sorted(Comparator.comparing(CYMaxFullLoadBean::getMaxSaturation).reversed()).limit(10).collect(Collectors.toList());
        }
        return topTen;
    }

    @Override
    public List<AllFullLoadBean> allGroupFullLoad() {
        String url = StartupClass.config.getProperty("FullLoadClient_url");
        Mono<Map> allFullLoad =
                webClient.get().uri(url).retrieve().bodyToMono(Map.class)
                        .doOnError(WebClientResponseException.class, err -> {
                            throw new RuntimeException("allGroupFullLoad error，error Message:"+err.getMessage());
                        });
        List<AllFullLoadBean> data = JSON.parseArray(JSON.toJSONString(allFullLoad.block().get("data")), AllFullLoadBean.class);
        if(allFullLoad.block().size() > 0){
            data.stream()
                    .forEach(item -> {
                        BigDecimal ffltSaturation = item.getFfltSaturation();
                        if (ffltSaturation.compareTo(new BigDecimal("0.1")) < 0) {
                            item.setFrontLineSize(20);
                            item.setLevel(1);
                        }else if (ffltSaturation.compareTo(new BigDecimal("0.6")) < 0) {
                            item.setFrontLineSize(50);
                            item.setLevel(2);
                        }else if (ffltSaturation.compareTo(new BigDecimal("0.8")) < 0) {
                            item.setFrontLineSize(60);
                            item.setLevel(3);
                        } else if (ffltSaturation.compareTo(new BigDecimal("0.8")) >= 0) {
                            item.setFrontLineSize(80);
                            item.setLevel(4);
                        }
                    });
        }
        return data;
    }
}
